package com.sys.platform.controller;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sys.platform.common.domain.AjaxResult;
import com.sys.platform.entity.GateRequest;
import com.sys.platform.service.BarrierGateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Api(tags = "道闸管理")
@RestController
@RequestMapping("barrierGate")
public class BarrierGateController {

    @Resource
    private BarrierGateService barrierGateService;

    @ApiOperation("打开道闸")
    @GetMapping("open")
    public AjaxResult openBarrierGate(GateRequest gate) {
        barrierGateService.openBarrierGate(gate);
        return AjaxResult.success();
    }

    @ApiOperation("关闭道闸")
    @GetMapping("close")
    public AjaxResult close(GateRequest gate) {
        barrierGateService.close(gate);
        return AjaxResult.success();
    }

    @ApiOperation("白名单列表")
    @GetMapping("whiteList")
    public AjaxResult whiteList(GateRequest gate) {
        List<Map<String, Object>> list = barrierGateService.whiteList(gate);
        return AjaxResult.success(list);
    }

    @ApiOperation("添加白名单")
    @GetMapping("addWhiteList")
    public AjaxResult addWhiteList(GateRequest gate) {
        barrierGateService.addWhiteList(gate);
        return AjaxResult.success();
    }

    @ApiOperation("删除白名单")
    @GetMapping("delWhiteList")
    public AjaxResult delWhiteList(GateRequest gate) {
        barrierGateService.delWhiteList(gate);
        return AjaxResult.success();
    }

    @ApiOperation("启用")
    @GetMapping("enable")
    public AjaxResult enable(GateRequest gate) {
        barrierGateService.enable(gate);
        return AjaxResult.success();
    }

    @ApiOperation("禁用")
    @GetMapping("disabled")
    public AjaxResult disabled(GateRequest gate) {
        barrierGateService.disabled(gate);
        return AjaxResult.success();
    }

    @ApiOperation("统计")
    @GetMapping("count")
    public AjaxResult count() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        Date date = new Date();
        String timestamp = sdf.format(date);
        String appId = "E0117FB586BA4526B359AD9D355BAADA";
        String secretKey = "B96BDD756928494BADD1429B18791E90";
        Map<String, Object> dateMap = getDate();
        // 将JSON对象转换为字符串
        ObjectMapper objectMapper = new ObjectMapper();
        String jsonString = null;
        try {
            jsonString = objectMapper.writeValueAsString(dateMap);
        } catch (Exception e) {
            throw new RuntimeException("查询失败");
        }
        // 使用Base64进行加密
        String encoded = Base64.getEncoder().encodeToString(jsonString.getBytes());

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("message_type", "screenStatistics");
        paramMap.put("data", encoded);
        paramMap.put("dataType", "json");
        String body = JSONUtil.toJsonPrettyStr(paramMap);

        String sign = DigestUtil.md5Hex(appId + secretKey + timestamp + body);
        HttpRequest request = HttpRequest.post("http://59.57.242.46:8890/bi-statistics/statistics");
        // 设置请求头部
        request.header("Content-Type", "application/json");
        request.header("appId", appId);
        request.header("timestamp", timestamp);
        request.header("sign", sign);
        // 设置请求体数据
        request.body(body);
        // 发送请求并获取响应
        HttpResponse response = request.execute();
        String str = response.body();

        List<Object> list = JSONObject.parseObject(str).getJSONArray("result");
        list.stream().forEach(item -> {
            System.out.println(JSONObject.parseObject(item.toString()).getString("date"));
        });
        return AjaxResult.success(JSONObject.parseObject(str).getJSONArray("result"));
    }

    public Map<String, Object> getDate() {
        Map<String, Object> map = new HashMap<>();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        //获取当前日期
        Date date = new Date();
        String endDate = formatter.format(date);
        map.put("endDate", endDate);

        Date newDate = DateUtil.offset(date, DateField.MONTH, -5);
        String str = formatter.format(newDate);
        String startDate = str.substring(0, str.length() - 2) + "01";
        map.put("startDate", startDate);
        return map;
    }

}
